﻿using System;
using System.Linq;
using YYTea.MOdel.Model;
using YYTea.ORM;

namespace YYTea.DbIntializer
{
    public class YYTeaDbInitiazer
    {
        public YYTeaDbInitiazer()
        {

        }
        public static void Initializer(YYTeaContext context)
        {
            context.Database.EnsureCreated();

            if (context.Roles.Any())
            {
                return;
            }
            var roles = new Role[]
            {
                new Role{ Num = 1 ,Name = "超级管理员" },
                new Role{ Num = 2 ,Name = "管理员" },
                new Role{ Num = 3 ,Name = "普通用户" }
            };
            context.Roles.AddRange(roles);
            context.SaveChanges();

            if (context.UserInfos.Any())
            {
                return;
            }
            var users = new UserInfo[]
            {
                new UserInfo {  UserName = "Admin" , Password = "123456" , Email = "123456@qq.com" , Address = "宿舍" ,
                    AddTime = Convert.ToDateTime("2020/5/5"), Mobile = "18888888888"  ,  Qq = "123456789",Sex = Sex.男,
                    RoleName = context.Roles.SingleOrDefault(s => s.Num==1).Name},
                new UserInfo { UserName = "01" , Password = "147258" , Email = "147258@qq.com" , Address = "宿舍01" ,
                    AddTime = Convert.ToDateTime("2020/5/5"), Mobile = "17777777777"  , Qq = "147258369",Sex = Sex.女,
                    RoleName = context.Roles.SingleOrDefault(s => s.Num==3).Name}
            };
            context.UserInfos.AddRange(users);
            context.SaveChanges();

            if (context.UserRoles.Any())
            {
                return;
            }
            var userRoles = new UserRole[]
            {
                new UserRole { RoleID = context.Roles.SingleOrDefault(s=>s.Num == 1).RoleID,
                    UserInfoID = context.UserInfos.SingleOrDefault(s=>s.Email == "123456@qq.com").UserInfoID},
                new UserRole { RoleID = context.Roles.SingleOrDefault(s=>s.Num == 3).RoleID,
                    UserInfoID = context.UserInfos.SingleOrDefault(s=>s.Email == "147258@qq.com").UserInfoID}
            };
            context.UserRoles.AddRange(userRoles);
            context.SaveChanges();

            if (context.ShoppingCars.Any())
            {
                return;
            }
            var shoppingCar = new ShoppingCar[]
            {
                new ShoppingCar{ SizeNum = 1, UserInfoID = context.UserInfos .SingleOrDefault(s=>s.Email == "123456@qq.com").UserInfoID},
                new ShoppingCar{ SizeNum = 2, UserInfoID = context.UserInfos .SingleOrDefault(s=>s.Email == "147258@qq.com").UserInfoID }

            };
            context.ShoppingCars.AddRange(shoppingCar);
            context.SaveChanges();

            if (context.Products.Any())
            {
                return;
            }
            var products = new Product[]
            {
                new Product { Name = "烤奶", Price= 25, Num  = 1, Detail = "唇齿留香", Img = "~/youyouMiljyTea/Img/烤奶.jpg"/*, ShoppingCarID = context.ShoppingCars.SingleOrDefault(s=>s.SizeNum == 1).ShoppingCarID*/},
                new Product { Name = "芒果汁", Price= 15, Num  = 10, Detail = "果香浓厚", Img = "~/youyouMiljyTea/Img/芒果汁.jpg"/*,ShoppingCarID = context.ShoppingCars.SingleOrDefault(s=>s.SizeNum == 1).ShoppingCarID*/}
            };
            context.Products.AddRange(products);
            context.SaveChanges();


            if (context.Orders.Any())
            {
                return;
            }
            var orders = new Order[]
            {
                new Order {  Money = 20 , AddDate = Convert.ToDateTime("2020/10/29"), state = "未支付",
                    ShoppingCars = context.ShoppingCars.SingleOrDefault(s=>s .SizeNum == 1),
                UserID = context.UserInfos.SingleOrDefault(s=>s.Email == "123456@qq.com").UserInfoID},
                new Order {  Money = 20 , AddDate = Convert.ToDateTime("2020/10/29"), state = "未支付",
                    ShoppingCars = context.ShoppingCars.SingleOrDefault(s=>s .SizeNum == 2),
                UserID = context.UserInfos.SingleOrDefault(s=>s.Email == "147258@qq.com").UserInfoID }

            };
            context.Orders.AddRange(orders);
            context.SaveChanges();

        }

    }
}
